package com.frogs.ucenter.service.daoimpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.base.dao.BaseDao;
import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.framework.util.PageList;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.SubFlowInfoDao;
import com.frogs.ucenter.dao.TotalFlowRelationDao;
import com.frogs.ucenter.entity.SubFlowInfo;
import com.frogs.ucenter.entity.SubFlowInfoExt;
import com.frogs.ucenter.entity.TotalFlowRelation;
import com.frogs.ucenter.service.SubFlowInfoService;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service("subFlowInfoService")
public class SubFlowInfoServiceImpl extends BaseServiceImpl<SubFlowInfo> implements SubFlowInfoService {
	@Autowired
    private SubFlowInfoDao subFlowInfoDao;
	
	@Autowired
    private TotalFlowRelationDao totalFlowRelationDao;

    public SubFlowInfoServiceImpl() {
        super();
    }

    public SubFlowInfoDao getSubFlowInfoDao() {
        return subFlowInfoDao;
    }

    /**
     * Don't remove or edit it.
     */
    @Override
    protected BaseDao<SubFlowInfo> getBaseDao() {
        return subFlowInfoDao;
    }

    public void setSubFlowInfoDao(SubFlowInfoDao subFlowInfoDao) {
        this.subFlowInfoDao = subFlowInfoDao;
    }
    
    @Override
    public List<SubFlowInfoExt> getSubFlowInfoExtList(Map<String, Object> params) {
		return this.subFlowInfoDao.selectLeftJoinByMap(params);
	}

	@Override
	public void insertRelation(TotalFlowRelation totalFlowRelation) {
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("eqSubFlowId", totalFlowRelation.getSubFlowId());
		params.put("eqTotalFlowId", totalFlowRelation.getTotalFlowId());
		PageList<TotalFlowRelation> list = totalFlowRelationDao.selectByMap(params, 1, 1, false);
		if(list.size() == 0){
			totalFlowRelationDao.insert(totalFlowRelation);
		}else{
			throw new ServiceOperationException("该子线已经存在,不能再添加");
		}
	}

	@Override
	public int deleteRelation(Map<String, Object> param) {
		// TODO Auto-generated method stub
		return totalFlowRelationDao.deleteByMap(param);
	}

	@Override
	public boolean isExistsTaskRelation(int flowId, int sequence) {
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("eqTotalFlowId", flowId);
		params.put("eqSequence", sequence);
		if(totalFlowRelationDao.selectCountByMap(params)>0){
			return true;
		};
		return false;
	}
    
}